package yhp.com.zijie.web;

import yhp.com.zijie.bean.Menu;
import yhp.com.zijie.bean.Role;
import yhp.com.zijie.bean.Users;
import yhp.com.zijie.service.MenuService;
import yhp.com.zijie.service.RoleService;
import yhp.com.zijie.service.UsersService;
import yhp.com.zijie.service.impl.MenuServiceImpl;
import yhp.com.zijie.service.impl.RoleServiceImpl;
import yhp.com.zijie.service.impl.UsersServiceImpl;
import yhp.com.zijie.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@WebServlet(urlPatterns = "/power/role/roles")
public class RoleServlet extends HttpServlet {


    private RoleService roleService = new RoleServiceImpl();
    private MenuService menuService = new MenuServiceImpl();
    private PageUtil pageUtil = new PageUtil();


    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String method = req.getParameter("method");
        if ("select".equals(method)){
            selectRole(req , resp);
        }else if ("getMenus".equals(method)){
            getMenus(req , resp);
        }else if ("insertRole".equals(method)){
            insertRole(req , resp);
        }
    }


    protected void insertRole(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        String roleName = req.getParameter("roleName");
        String[] menuIds = req.getParameterValues("menuId");
        String state = req.getParameter("state");
        System.out.println(roleName);
        System.out.println(state);

        for (String menuId : menuIds) {
            System.out.println(menuId);
        }
        System.out.println("===============================");

        int i = roleService.insertRole(roleName, state, menuIds);

        if (i>0){
            resp.sendRedirect("/power/role/roles?method=select");
        }else {
            resp.sendRedirect("/power/role/roles?method=getMenus");
        }
    }



    protected void getMenus(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        List<Menu> menuList = menuService.getMenus();

        req.setAttribute("menuList" , menuList);

        req.getRequestDispatcher("add.jsp").forward(req , resp);

    }


    protected void selectRole(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //当前页
        String index = req.getParameter("index");
        int pageIndex = (index==null||index.length()==0)?1:Integer.parseInt(index);

        //调用方法(查询用户列表， 查询总条数)


        //查询用户列表
        List<Role> roleList = roleService.getRoles(pageIndex, pageUtil.getPageSize());

        //查询总条数
        int total = roleService.total();

        //赋值给pageUtil工具类
        pageUtil.setPageIndex(pageIndex);
        pageUtil.setTotal(total);
        pageUtil.setDataList(roleList);

        //存储值，传给前端
        req.setAttribute("pi" , pageUtil);

        //跳转页面
        req.getRequestDispatcher("list.jsp").forward(req , resp);
    }


}
